import Vue from 'vue'
import VueRouter from 'vue-router'

// user
import User from './modules/user'
import Service from './modules/service'

// news
import News from './modules/news'
import Article from './modules/article'

// product
import Product from './modules/product'

// merchant
import Merchant from './modules/merchant'

// account
import Account from './modules/account'

// order
import Order from './modules/order'

// pay
import Pay from './modules/pay'

Vue.use(VueRouter)

// const appName = '千颜生物'
const appName = process.env.VUE_APP_NAME

const routes = [
  {
    path: '/',
    component: () => import(/* webpackChunkName: 'tabbar' */ '@/views/tabbar'),
    // redirect: '/home',
    redirect: '/index',
    children: [
      {
        path: 'index',
        component: resolve => require(['@/views/index'], resolve),
        meta: { title: appName }
      },
      {
        path: 'home',
        component: resolve => require(['@/views/home'], resolve),
        meta: { title: appName }
      },
      {
        path: 'group',
        component: resolve => require(['@/views/group'], resolve),
        meta: { title: '拼团' }
      },
      {
        path: 'category',
        component: resolve => require(['@/views/product/category'], resolve),
        meta: { title: '分类' }
      },
      {
        path: 'merchant',
        component: resolve => require(['@/views/merchant'], resolve),
        meta: { title: '商家' }
      },
      {
        path: 'material',
        component: resolve => require(['@/views/material'], resolve),
        meta: { title: '素材' }
      },
      {
        path: 'cart',
        component: resolve => require(['@/views/cart'], resolve),
        meta: { title: '购物车' }
      },
      {
        path: 'article',
        component: resolve => require(['@/views/article/list'], resolve),
        meta: { title: '种草' }
      },
      {
        path: 'center',
        component: resolve => require(['@/views/center'], resolve),
        meta: { title: '我的', noCache: false }
      },
      {
        path: 'avatar',
        component: resolve => require(['@/views/center/avatar'], resolve),
        meta: { title: '修改资料', noCache: false }
      }
    ]
  },
  Article,
  News,
  User,
  Service,
  Product,
  Merchant,
  Account,
  Order,
  Pay,
  {
    path: '/cache/:confirm?',
    component: resolve => require(['@/views/cache'], resolve),
    meta: { title: '清除缓存' }
  },
  {
    path: '/test',
    component: resolve => require(['@/views/test'], resolve),
    meta: { title: '测试使用' }
  },
  {
    path: '/search',
    component: resolve => require(['@/views/search'], resolve),
    meta: { title: '搜索' }
  },
  // merchant pay
  {
    path: '/merchant-pay/:id',
    component: resolve => require(['@/views/merchant/pay'], resolve),
    meta: { title: '商家收款' }
  },
  {
    path: '*',
    component: resolve => require(['@/views/error'], resolve),
    meta: { title: '404' }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

/**
 * 解决重复点击tabbar出现错误
 */
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

/**
 * 设置 html title
 */
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  } else {
    if (process.env.VUE_APP_TITLE !== 'undefined') {
      document.title = process.env.VUE_APP_TITLE
    } else {
      document.title = appName
    }
  }
  next()
})

export default router
